import { createRouter, createWebHashHistory } from 'vue-router'
import defaultRoutes from './default'
import routesRoutes from './routes'

const routes = [ ...defaultRoutes, ...routesRoutes ]

const router = createRouter({
  history: createWebHashHistory(), // 替代之前的mode，是必须的
  routes
})

// 进入路由前
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if (sessionStorage.getItem('TOKEN')) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

// 进入路由后
router.afterEach(() => {
  window.scrollTo(0, 0)
})

export default router
